/*
For each individual-year, determine whether they live in the same
municipality as either parent.

Uses:
data/admin/raw/living_64_20.dta
data/admin/raw/lnr_mlnr_flnr_updated2013.dta

Creates:
data/admin/intermediate/live_near_parents.dta
*/

cd "$root"

use data/admin/raw/living_64_20.dta, clear
keep if year >= 1995
ren location own_loc

merge m:1 lnr using data/admin/raw/lnr_mlnr_flnr_updated2013.dta, keep(3) nogen
* mother's location
ren (lnr mlnr) (lnr_orig lnr)
merge m:1 lnr year using data/admin/raw/living_64_20.dta, keep(1 3) nogen
ren (location lnr flnr) (loc_mlnr mlnr lnr)
* father's location
merge m:1 lnr year using data/admin/raw/living_64_20.dta, keep(1 3) nogen
ren (location lnr lnr_orig) (loc_flnr flnr lnr)

gen live_near_parents = 0
replace live_near_parents = 1 if (own_loc == loc_mlnr | own_loc == loc_flnr) & own_loc != ""

keep lnr year live_near_parents

save data/admin/intermediate/live_near_parents.dta, replace
